import request from '@/router/axios';
import Mock from 'mockjs';
import { Random } from 'mockjs'

//商品名称 和图片
// 创建自定义 Mock 函数
Random.extend({
      // 自定义函数名: function 函数  
      fruit: function() {    
        const arr = [
            '樱の色烟酰胺润肤补水五件套提拉控油保湿滋润面部套盒护肤品套装',
            '肤姿美蜂蜜补水保湿面膜控油滋润嫩滑毛孔细致护肤化妆品面膜',
            '束渍油污净 厨房油烟机清洗剂去重油污清洁剂除油剂贴牌定制',
            '束渍香水洗衣液 女士洗衣露内衣贴身衣物持久留香不伤衣物',
            '束渍薰衣草香水洗衣液2kg厂家OEM 贴牌代加工定制',
            '洗洁精1.2kg大桶厨房餐具洗涤剂果蔬净工厂直销洗碗液',
            '束渍2kg香水洗衣液瓶装贴牌代工OEM厂家直销正品赠品持久留香',
            '高端洗洁精柠檬香型餐具清洁剂洗碗液洗涤剂贴牌厂家直销oem',
            '【正品】束渍椰油洗衣液2kg  厂家直销 现货 OEM代工贴牌',
            '【正品包邮】香水洗衣液2kg 持久留香厂家直销oem贴牌代加工'
        ]
        return this.pick(arr)  
    }
})
Random.extend({
    // 自定义函数名: function 函数  
    imgFn: function() {    
      const arr = [
          'https://cbu01.alicdn.com/img/ibank/2020/582/018/18258810285_1442181519.400x400.jpg',
          'https://cbu01.alicdn.com/img/ibank/2018/419/635/8985536914_1442181519.400x400.jpg',
          'https://cbu01.alicdn.com/img/ibank/2020/181/066/18940660181_220139942.400x400.jpg',
          'https://cbu01.alicdn.com/img/ibank/2020/111/468/23561864111_220139942.400x400.jpg',
          'https://cbu01.alicdn.com/img/ibank/O1CN013pVl4d1OtUuVKbVNe_!!6000000001763-0-cib.400x400.jpg',
          'https://cbu01.alicdn.com/img/ibank/2020/114/085/22581580411_220139942.400x400.jpg',
          'https://cbu01.alicdn.com/img/ibank/2020/150/057/23561750051_220139942.400x400.jpg',
          'https://cbu01.alicdn.com/img/ibank/2020/375/529/18872925573_220139942.400x400.jpg',
          'https://cbu01.alicdn.com/img/ibank/2020/618/407/17934704816_220139942.400x400.jpg',
          'https://cbu01.alicdn.com/img/ibank/2020/019/086/22122680910_220139942.400x400.jpg'
      ]
      return this.pick(arr)  
  }
})
//@fruit()随机生成 商品名称 
//@imgFn()随机生成 图片

let list = Mock.mock({
    "tableList|10-20":[
        {
            id:'@id',     //商品编号
            goodsname:'@fruit()',  //商品名称
            picUrl:'@imgFn()',
            "price1|1-60.2": 1,   //专柜价格
            "price2|1-60.2": 1,   // 当前价格
            "status|0-2":10,   //审批状态  0 通过  1 待审批  2 不通过
        }
    ]
})

// 获取商品列表
export const goodsList = (data) =>request({
    url:'/goodsList',
    method:'get',
    data
})
Mock.mock(/\/goodsList/,config=>{
    let {pagesize,pagenum} = JSON.parse(config.body);
    let _list = list.tableList;
    //总条数
    let total = _list.length;
    //返回当前页数的对应的pagesize数量的数据
    if(pagesize && pagenum){
        let user = _list.filter((item,index)=>index<pagesize*pagenum && index>=pagesize*(pagenum-1));
        return {
            data:{
                user,total,pagenum,pagesize
            },
            meta:{
                msg:'请求成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'获取失败',
                status:400
            }
        }
    }
})

//添加商品
export const addgoods = (data) =>request({
    url:'/addgoods',
    mothed:'post',
    data
});
Mock.mock(/\/addgoods/,config=>{
    var ids = Mock.mock({id:'@id'});
    let datas = JSON.parse(config.body)
    let {goodsname,price1,price2} = datas;
    let _list = list.tableList;
    if(goodsname && price1 && price2){
        _list.unshift({
            id:ids.id,
            goodsname,
            picUrl:datas.img?datas.img:'',
            price1:price1,  
            price2:price2,   
            status:1,
        })
        return {
            meta:{
                msg:'添加成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'添加失败',
                status:400
            }
        }
    }
    
})

//删除商品
export const detgoods = (data) =>request({
    url:'/detgoods',
    mothed:'delete',
    data
})
Mock.mock(/\/detgoods/,config=>{
    let {id} = JSON.parse(config.body);
    let _list = list.tableList;
    if(id){
        for(let index in _list){
            if(_list[index].id==id){
                _list.splice(index,1)
            }
        }
        return {
            meta:{
                msg:'删除成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'删除失败',
                status:400
            }
        }
    }
    
})

//根据id 商品名称 审批状态 查询商品
export const querygoods = (data) =>request({
    url:'/querygoods',
    mothed:'get',
    data
})
Mock.mock(/\/querygoods/,config=>{
    let datas = JSON.parse(config.body);
    let _list = list.tableList;
    let arr = [];
    if(datas.id || datas.goodsname || datas.status){
        if(datas.id){
            arr = _list.filter((item,index)=>item.id.indexOf(datas.id)!=-1);
        }
        if(datas.goodsname){
            arr = (arr.length==0?_list:arr).filter((item,index)=>item.goodsname.indexOf(datas.goodsname)!=-1);
        }
        if(datas.status){
            let str = JSON.stringify(datas.status)
            arr = (arr.length==0?_list:arr).filter((item,index)=>str.indexOf(item.status)>=0);
        }  
        let total = arr.length;
        let pagesize = arr.length
        return {
            data:{
                arr,total,pagesize
            },
            meta:{
                msg:'获取成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'参数错误',
                status:400
            }
        }
    } 
})

// 修改商品数据
export const editgoods = (data) =>request({
    url:'/editgoods',
    mothed:'put',
    data
})
Mock.mock(/\/editgoods/,config=>{
    let datas = JSON.parse(config.body);
    let _list = list.tableList;
    let {id} = datas;
    if(id){
        for(let item of _list){
            if(item.id==id){
                item.goodsname=datas.goodsname?datas.goodsname:item.goodsname;
                item.picUrl=datas.img?datas.img:item.picUrl;
                item.price1=datas.price1;
                item.price2=datas.price2;
            }
        }
        return {
            meta:{
                msg:'修改成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'参数错误',
                status:400
            }
        }
    }
   
})
